package com.bxd.baodan.activity;

import android.graphics.Color;
import android.text.SpannableString;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.bxd.baodan.R;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.item.GanHuoBean;
import com.bxd.baodan.util.GlideUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.KeyWordUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.widget.SearchView;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by lizhichao on 18/1/15.
 */

public class SearchArticleActivity extends BaseListActivity<GanHuoBean>{
    private SearchView searchView;
    private TextView count_dec;
    private String key;

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected void preDone() {
        setSearchBar();

        key = getIntent().getStringExtra("key");
        searchView = findViewById(R.id.search_view);
        count_dec = findViewById(R.id.count_dec);

        searchView.setSearchViewListener(new SearchView.SearchViewListener() {
            @Override
            public void textChanged(String text) {

            }

            @Override
            public void onSearch(String text, int cate_id) {
                key = text;
                netUtil.setParams("keyword", key);
                autoRefresh();

            }

            @Override
            public void onEmpty() {

            }
        });
        searchView.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        searchView.et.setText(key);
        autoRefresh();

    }


    @Override
    public void setNetUtil() {

        netUtil = new NetUtil(this, JsonApi.article_search);
        netUtil.setParams("keyword", key);
    }

    @Override
    protected Type getTypeToken() {
        return new TypeToken<List<GanHuoBean>>() {
        }.getType();
    }


    @Override
    public int getItemLayout() {
        return R.layout.gan_huo_item;
    }

    @Override
    public void convert(ViewHolder holder, final GanHuoBean item) {
        GlideUtil.loadImage(this, item.image, ((ImageView) holder.getView(R.id.image)));

        holder.setText(R.id.title, item.title);
        holder.setText(R.id.read, item.viewCount + "阅读");

        holder.getView(R.id.main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{item.url});
            }
        });



    }

    @Override
    protected String getArrayStr(String json_data) {
        String count = JSONUtil.getData("count", json_data);
        String countStr = "共搜到" + count + "条 “" + key + "” 相关信息";

        final SpannableString keyDec = KeyWordUtil.matcherSearchTitle(Color.parseColor("#16c571"), countStr, key);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                count_dec.setText(keyDec);
            }
        });


        return JSONUtil.getArrayData("list", json_data);
    }
}
